/** Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.*/
package com.truba.ohostouchgallery.slice;

import com.truba.ohostouchgallery.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;

import ru.truba.touchgrallery.GalleryWidget.BasePagerAdapter;
import ru.truba.touchgrallery.GalleryWidget.FilePagerAdapter;
import ru.truba.touchgrallery.GalleryWidget.GalleryViewPager;
import ohos.agp.window.dialog.ToastDialog;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;
import ohos.global.resource.Entry;
import ohos.global.resource.Resource;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class GalleryFileActivitySlice extends AbilitySlice {
    private GalleryViewPager mViewPager;
    ToastDialog toast;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_main);
        Entry[] urls = null;
        List<String> items = new ArrayList<String>();
        try {
         urls=  getResourceManager().getRawFileEntry("resources/rawfile").getEntries();
            for (Entry filename : urls)
            {
                if (filename.getPath().matches(".+\\.jpg"))
                {
                    String path = getFilesDir() + "/" + filename.getPath();
//                    System.out.print(filename.getPath());
                    Resource resource=getResourceManager().getRawFileEntry("resources/rawfile/"+filename.getPath()).openRawFile();
                    File file=new File(path);
                    copy(resource, file);
                    items.add(path);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        FilePagerAdapter pagerAdapter = new FilePagerAdapter(this, items);
        pagerAdapter.setOnItemChangeListener(new BasePagerAdapter.OnItemChangeListener()
        {
            @Override
            public void onItemChange(int currentPosition)
            {
               InnerEvent event= InnerEvent.get();
                event.object=currentPosition;
                eventHandler.sendEvent(event);
            }
        });

        mViewPager = (GalleryViewPager)findComponentById(ResourceTable.Id_viewer);
      //  mViewPager.setOffscreenPageLimit(3);
        mViewPager.addPageChangedListener( pagerAdapter.new pagechangeList(mViewPager));
        mViewPager.setPageCacheSize(3);
        mViewPager.setProvider(pagerAdapter);
    }
    public void copy(InputStream in, File dst) throws IOException {

        OutputStream out = new FileOutputStream(dst);

        // Transfer bytes from in to out
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
EventHandler eventHandler=new EventHandler(EventRunner.getMainEventRunner())
{
    @Override
    protected void processEvent(InnerEvent event) {
        super.processEvent(event);
        if (toast==null)
        {
            toast=  new ToastDialog(getContext()) ;
        }
        int currentPosition= (int) event.object;
        toast.setText("Current item is " + currentPosition).show();
    }
};

    @Override
    protected void onStop() {
        super.onStop();

    }
}
